<%@ Language=VBScript %>
<!--#include file="..\..\RootContent.inc"-->
<%	
	set Conn=server.CreateObject("ADODB.Connection")
	Conn.Open Application("StrConn")
	nodemax=cint(Request("max"))
	nodeid=giaima(Request("id"))	
	IDFlow=request("IDFlow")
	Nguoitao=request("Nguoitao")
	'on Error Resume next
	Conn.BeginTrans
	'xoa file dinh kem va xoa file noi dung y kien
	Call DeleteNodeFile(nodeid)
	sql1="delete from FlowNode where Maworkflownode like '" & nodeid & "%'"
	conn.Execute sql1
	sSQL="Select * from workflow where nodecha like '"& nodeid &"%'"
	Set RecT=server.CreateObject("ADODB.Recordset")
	RecT.CursorType=3
	RecT.CacheSize=5
	RecT.Open sSQL,Conn
	Do while not RecT.EOF
		'xoa file dinh kem va xoa file noi dung y kien
		Call DeleteNodeFile(RecT("Maworkflow"))
		sSQL="Delete from Flownode where Maworkflow like '"& RecT("Maworkflow") &"'"
		Conn.Execute sSQL
		Call DeleteFile(RecT("Maworkflow"))
		RecT.MoveNext
	Loop
	RecT.Close
	Set RecT=nothing	
	sSQL="Delete from workflow where nodecha like '"& nodeid &"%'"
	Conn.Execute sSQL
	dim n,i,m,tempid
	n=cint(right(nodeid,2))
	Filename=request("Filename")	
	if nodemax>n then		
		for i=n+1 to nodemax
			'UPDATE FlowNode SET ID = "000013%" WHERE (((FlowNode.ID) Like "000013*"));
			if i<10 then
				id1=left(nodeid,len(nodeid)-2) & "0" & i
			else
				id1=left(nodeid,len(nodeid)-2)&i
			end if
			if i<11 then
				id2=left(nodeid,len(nodeid)-2) & "0" & (i-1)
			else
				id2=left(nodeid,len(nodeid)-2) & (i-1)
			end if			
			sql1="select Maworkflownode from flownode where Maworkflownode like '" & id1 & "%'"
			set rec=server.CreateObject("ADODB.Recordset")
			rec.Open sql1,conn,3
			Do while not rec.EOF
				id0=rec.Fields(0)
				sql1="update flownode set Maworkflownode='" & id2 & right(id0,len(id0)-len(id2))&"' where Maworkflownode='" & id0 & "'"
				'set rec1=server.CreateObject("ADODB.Recordset")
				'rec1.Open sql1,conn,3	
				Conn.Execute sql1
				rec.MoveNext
			loop
		next
	end if			
	if Err.number then
		Conn.RollbackTrans
	else
		Conn.CommitTrans
	end if
	conn.Close
	set conn=nothing
	bMaRoot=Request("bMaRoot")
	if Request("xulyflow")="ok" then
		lnk="../Editxulyflow.asp?bMaRoot="& bMaRoot &"&IDFlow="& IDFlow &"&Nguoitao="& Nguoitao
	else
		lnk="../flow.asp?bMaRoot="& bMaRoot &"&IDFlow="& IDFlow &"&Nguoitao="& Nguoitao
	end if
	Response.Redirect(lnk)
%>
<script language=vbscript runat=server>
		Function GiaiMa(msText) 
	    Dim nC
	    Dim nD
	    Dim nE
	    Dim lA
	    Dim lB
	    Dim lI
	    Dim lJ
	    Dim lK
	    Dim sB
	    lA = Len(msText)
	    lB = lA - 1 - (lA - 1) \ 4
	    sB = Space(lB)
	    For lI = 1 To lB
	        lJ = lJ + 1
	        nC = Asc(Mid(msText, lJ, 1)) - 59	        
	        Select Case lI Mod 3
	        Case 1
	            lK = lK + 4
	            If lK > lA Then lK = lA
	            nE = Asc(Mid(msText, lK, 1)) - 59
	            nD = ((nE \ 16) And 3) * 64
	        Case 2
	            nD = ((nE \ 4) And 3) * 64
	        Case 0
	            nD = (nE And 3) * 64
	            lJ = lJ + 1
	        End Select
	        sB=Mid(sB,1,lI-1) & Chr(nC Or nD) & Mid(sB,lI+1,len(sB)-lI)
	    Next
	    GiaiMa = sB
	End Function
</script>
<Script language=vbscript runat=server>
	'Khi xoa 1 workflow thi xoa kem theo File vanban cua Workflow dong thoi xoa danh sach cac file van ban gan kem
	Function DeleteFile(bIDFlow)
		sSQL="Select Tentep from Workflow_Tentep where Maworkflow = '"& bIDFlow &"'"
		Set RecXoa =server.CreateObject("ADODB.Recordset")
		RecXoa.CursorType=3
		RecXoa.CacheSize=5
		RecXoa.Open sSQL,Conn
		set Obj=server.CreateObject("ActiveFile.File")
		Do while not RecXoa.EOF
			Tentep=RecXoa("Tentep")
			obj.Name=RootContent & "Data\Yeucau\" & Tentep
			if obj.Exists() then
				obj.Delete()
			end if
			RecXoa.MoveNext
		Loop
		RecXoa.Close
		Set RecXoa=nothing
		TentepTEXT=bIDFlow & ".VT"
		obj.Name=RootContent & "Data\Yeucau\TEXT\" & TentepTEXT
		if obj.Exists() then
			obj.Delete()
		end if
		sSQL="Delete from workflow_Tentep where maworkflow = '"& bIDFlow &"'"
		Conn.Execute sSQL
		Set Obj=nothing
	End Function
	'22_1
	'Khi xoa 1 flownode thi xoa kem theo File vanban cua flownode dong thoi xoa danh sach cac file van ban gan kem
	Function DeleteNodeFile(bIDFlownode)
		'xoa file dinh kem
		sSQL="Select Tentep from flownode_Tentep where maflownode like '"& bIDFlownode & "%'"
		Set RecXoa =server.CreateObject("ADODB.Recordset")
		RecXoa.CursorType=3
		RecXoa.CacheSize=5
		RecXoa.Open sSQL,Conn
		set Obj=server.CreateObject("ActiveFile.File")
		Do while not RecXoa.EOF
			Tentep=RecXoa("Tentep")
			obj.Name=RootContent & "Data\Xuly\" & Tentep
			if obj.Exists() then
				obj.Delete()
			end if
			RecXoa.MoveNext
		Loop
		RecXoa.Close
		Set RecXoa=nothing
		sSQL="Delete from flownode_Tentep where maflownode like '"& bIDFlownode & "%'"
		Conn.Execute sSQL
		'xoa file noi dung y kien xu ly
		sSQL="Select maworkflownode from flownode where maworkflownode like '"& bIDFlownode & "%'"
		Set RecXoaND =server.CreateObject("ADODB.Recordset")
		RecXoaND.CursorType=3
		RecXoaND.CacheSize=5
		RecXoaND.Open sSQL,Conn
		set Obj=server.CreateObject("ActiveFile.File")
		Do while not RecXoaND.EOF
			TentepTEXT=RecXoaND("maworkflownode") & ".VT"
			obj.Name=RootContent & "Data\Xuly\Text\" & TentepTEXT
			if obj.Exists() then
				obj.Delete()
			end if
			RecXoaND.MoveNext
		Loop
		RecXoaND.Close
		Set RecXoaND=nothing
		Set Obj=nothing
	End Function
</Script>